//
//  WanTabBarController.m
//  MusicRunner
//
//  Created by Wan on 15/12/2.
//  Copyright © 2015年 WanWanSui. All rights reserved.
//

#import "THTabBarController.h"
#import "THView.h"
#import "THLabel.h"


@interface THTabBarController ()
@property (nonatomic, retain) UIButton *selectedButton;
@property(nonatomic,retain)UIButton *lastButton;//记录上一个button
@property(nonatomic,retain)UIImageView *headerV;//自定义按钮
@property(nonatomic,retain)NSTimer *timer;//时间计时器
@property(nonatomic,retain)UIImage *image3;
@property(nonatomic,retain)THView *myView;
@end

@implementation THTabBarController
#define CGRectMake(A, B, C, D) \
CGRectMake(A, B, C, D)
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGRect rect = self.tabBar.frame;
    [self.tabBar removeFromSuperview];
    //添加自定义tabBar
    self.myView = [[THView alloc] initWithFrame:rect];
    self.myView.alpha = 0.5;
    self.myView.backgroundColor = [UIColor colorWithRed:230 / 255.0 green:230 / 255.0 blue:230 / 255.0 alpha:1];
    self.myView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.myView];
    //按钮一
    UIButton *travel = [[UIButton alloc] init];
    travel.frame = CGRectMake(0, -5 * XHIGHT, self.myView.frame.size.width / 4, 50 * XHIGHT);
    [travel setImage:[UIImage imageNamed:@"travel"] forState:0];
    UIImage *image1 = [UIImage imageNamed:@"traveled"];
    image1 = [image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [travel setImage:image1 forState:UIControlStateSelected];
    travel.tag = 10001;
    travel.selected = YES;
    [travel addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.myView addSubview:travel];

    //按钮2
    UIButton *music = [[UIButton alloc] init];
    music.frame = CGRectMake(self.myView.frame.size.width / 4, -5 * XHIGHT, self.myView.frame.size.width / 4, 50 *XHIGHT);
    [music setImage:[UIImage imageNamed:@"raider"] forState:0];
    UIImage *image2 = [UIImage imageNamed:@"raidered"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [music setImage:image2 forState:UIControlStateSelected];
    music.tag = 10002;
    [music addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:music];
    
    //按钮3
    UIButton *toolB = [[UIButton alloc] init];
    toolB.frame = CGRectMake(self.myView.frame.size.width / 2, -5 * XHIGHT, self.myView.frame.size.width / 4, 50*XHIGHT);
    [toolB setImage:[UIImage imageNamed:@"toolBox"] forState:0];
    UIImage *image3 = [UIImage imageNamed:@"toolBoxed"];
    image3 = [image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [toolB setImage:image3 forState:UIControlStateSelected];
    toolB.tag = 10003;
    [toolB addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:toolB];

    //按钮4
    UIButton *search = [[UIButton alloc] init];
    search.frame = CGRectMake(self.myView.frame.size.width / 4 * 3, -5 * XHIGHT, self.myView.frame.size.width / 4, 50*XHIGHT);
    [search setImage:[UIImage imageNamed:@"search"] forState:0];
    UIImage *image4 = [UIImage imageNamed:@"searched"];
    image4 = [image4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [search setImage:image4 forState:UIControlStateSelected];
    search.tag = 10004;
    [search addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.myView addSubview:search];

    NSMutableArray *array=[NSMutableArray arrayWithObjects:@"游记",@"攻略",@"工具箱",@"搜索", nil];
    for (int i = 0; i < 4; i++) {
        UILabel *lab = [[THLabel alloc]initWithFrame:CGRectMake(self.myView.frame.size.width / 4 * i, 34, self.myView.frame.size.width / 4, 17)];
        lab.tag = 1001 + i;
        lab.text = [array objectAtIndex:i];
        lab.textColor = [UIColor whiteColor];
        if (lab.tag == 1001) {
            lab.textColor =[UIColor colorWithRed:191 / 255.0 green:85 / 255.0 blue:236 / 255.0 alpha:1];
        }
        lab.textAlignment = 1;
        [lab setFont:[UIFont fontWithName:@"Helvetica-Bold" size:11]];
        [self.myView addSubview:lab];
    }

}

-(void)buttonAction:(UIButton *)btn
{
    UINavigationController * navC = (UINavigationController *)[self.viewControllers objectAtIndex:btn.tag - 10001];
    [navC popToRootViewControllerAnimated:YES];
    self.headerV.image = self.image3;
    self.lastButton.selected = NO;
    for (int i = 10001; i < 10005; i ++) {
        UIButton *button = [self.view viewWithTag:i];
        button.selected = NO;
    }
    for (int i = 1001; i < 1005; i++) {
        UILabel *label = [self.view viewWithTag:i];
        label.textColor = [UIColor whiteColor];
    }
    UILabel *label = (UILabel *)[self.view viewWithTag:btn.tag - 9000];
    label.textColor = [UIColor colorWithRed:191 / 255.0 green:85 / 255.0 blue:236 / 255.0 alpha:1];
    btn.selected = YES;
    self.lastButton = btn;
    self.selectedIndex = btn.tag - 10001;
}

-(void)tapAction
{
    
    self.lastButton.selected = NO;
    UIImage *image = [UIImage imageNamed:@"Playing"];
    self.headerV.image = image;
    
//    PlayingViewController *playVC = [PlayingViewController sharePlayingViewController];
//    
//    [self presentViewController:playVC animated:YES completion:^{
//        
//        
//    }];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
